function dt=var_select_neighbor_val(dt,maxvar,mis)

%VAR_SELECT_NEIGHBOR_VAL double validation on variable selection
%Syntax: dt=var_select_neighbor_val(dt)
%Description: leave-one-out validation of leave-one-out crossvalidated
%             variable selection.
% 
%Jonas Almeida 18 Feb 2006
if nargin<3;mis='';end % mis is used to to pass text to the progress bar
x=dt.nx;y=dt.y;
[n,m]=size(dt.x);
if nargin<2
    maxvar=m;
elseif isempty(maxvar)
    maxvar=m;
else
    maxvar=min([maxvar,n]);
end
k=length(dt.y(1,:));
dt.predict.var=[];
dt.predict.dy=[];
h=waitbar(0,'Initializing new NRN committee');
set(get(get(h,'Children'),'Title'),'String','Committee expert recruitment:','HorizontalAlignment','left','VerticalAlignment','baseline','Position',[1,2.7,1],'FontSize',8,'FontName','Verdana');
set(h,'Name',mis)
for i=1:n
    xi=x;yi=y;
    xi(i,:)=[];yi(i,:)=[];
    [var,dy]=var_select_neighbor(xi,yi,maxvar);
    dt.predict.var=[dt.predict.var;var];
    dt.predict.dy=[dt.predict.dy;dy];
    waitbar(i/n,h,['Committee expert panel recruited:\newline',sprintf('[%s] ',dt.vars{dt.predict.var(i,1:min([10,maxvar]))})]);
    %disp([num2str(i),': var selected / cv error'])
    %disp(var(1:10));disp(dy(1:10));
    for j=1:maxvar %for each model make a prediction
        dt.predict.y(i,j)=neighbors_y(xi(:,var(1:j)),yi,x(i,var(1:j)));
    end
    %figure;s=find(yi==1);n=find(yi==0);plot3(xi(n,var(1)),xi(n,var(2)),xi(n,var(3)),'o','MarkerFaceColor','g');hold on;plot3(xi(s,var(1)),xi(s,var(2)),xi(s,var(3)),'o','MarkerFaceColor','r');grid on;xlabel('Var 1');plot3(x(i,var(1)),x(i,var(2)),x(i,var(3)),'*');
    %title([num2str(i),': should be ',num2str(y(i)),' predicted: ',num2str(dt.predict.y(i,1:3))])
    dt.predict.err(i,:)=~(dt.predict.y(i,:)==dt.y(i));
end
close(h)
